/*
 * Copyright 2006 Robert Burrell Donkin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package rat;

import rat.license.standard.LicenseCollationVisitor;

/**
 * <p>Simple application that checks files for license headers.</p>
 * <p>
 * Recipe: recursely check every file with a directory structure
 * for something that looks like a license header 
 * then report the results of these checks in a useful 
 * human readable fashion. Only human readable files must have license
 * header so this check is not as hard as it might first seem.
 * The check will analyse the file line-by-line searching for a match
 * of the first line of the ASL anywhere within that line. 
 * </p>
 */
public class LicenseChecker {

	private final Directory base;
	
	LicenseChecker(final Directory base) {
		this.base = base;
	}
	
	public void report(IPrinter printer) {
		doWork();
		printer.printLine("");
        printSummary(printer);
        printer.printLine("");
        printer.printLine("");
		printer.printLine("Analysing Documents...");
		printer.printLine("  Files with ASL headers will be marked L");
		printer.printLine("  Binary files (which do not require ASL headers) will be marked B");
		printer.printLine("  Compressed archives will be marked A");
		printer.printLine("  Notices, licenses etc will be marked N");
		printResults(printer);
		printer.printLine("");
		printer.printLine("");
		printer.printLine("Printing headers for files without ASL header...");
		printViolations(printer);
	}
	
	private void doWork() {
		Engine engine = new Engine();
		Recruiter recruiter = new Recruiter(engine);
		DirectoryTraverser traverser = new DirectoryTraverser(recruiter);
		base.accept(traverser);
		engine.doAllWork();
	}
	
	private void printResults(final IPrinter printer) {
		ToStringElementVisitor visitor = new ToStringElementVisitor(printer);
		DirectoryTraverser traverser = new DirectoryTraverser(visitor);
		base.accept(traverser);
	}
    
    private void printSummary(final IPrinter printer) {
        final LicenseCollationVisitor collator = new LicenseCollationVisitor();
        final SummaryVisitor visitor = new SummaryVisitor(collator);
        final DirectoryTraverser traverser = new DirectoryTraverser(visitor);
        base.accept(traverser);
        visitor.report(printer);
    }
	
	private void printViolations(final IPrinter printer) {
		final ViolationPrinterVisitor visitor = new ViolationPrinterVisitor(printer);
		final DirectoryTraverser traverser = new DirectoryTraverser(visitor);
		base.accept(traverser);
	}
}
